package com.cndw.kungfu.model.pvp.camp;

import java.util.Collection;
import java.util.List;

import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataSerialize;
import com.cndw.kungfu.vo.EventNotice;

public class CampWar extends CampWarBase {

	private static int endTime;

	public synchronized static void runBoss() {
		int currentTime = TimeProcesser.getUnixTime();
		int morningTime = TimeProcesser.getMorningTime();
		int startTime = morningTime + CampConfig.startTime;
		endTime = startTime + CampConfig.campLifeTime;

		if (endTime < currentTime) {
			startTime += 86400;
			endTime += 86400;
		}

		Context.getTimerServer().scheduleAtFixedRate(new Runnable() {

			@Override
			public void run() {
				try {
					endTime = TimeProcesser.getMorningTime() + CampConfig.startTime + CampConfig.campLifeTime;
					start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, startTime - currentTime, 86400);

		Context.getTimerServer().scheduleAtFixedRate(new Runnable() {

			@Override
			public void run() {
				try {
					stop();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, endTime - currentTime, 86400);

	}

	public synchronized static void debug() {
		if (!open) {
			start();
		}
	}

	private synchronized static void start() {
		if (vsTask != null) {
			vsTask.cancel(false);
		}
		clearOnStart();
		autoJoin();
		open = true;
		CampWarEvent warEvent = new CampWarEvent();
		vsTask = Context.getTimerServer().scheduleAtFixedRate(warEvent, 30, 30);
	}

	static void autoJoin() {
		int joinSize = 200;

		List<Long> heroList = DataSerialize.getInstance().getChallengeData().getPlayers(1, joinSize);
		for (Long targetId : heroList) {
			if (!isJoin(targetId)) {
				Player targetPlayer = Context.getPlayerServer().getByPlayerId(targetId);
				if (targetPlayer != null && targetPlayer.getLevel() >= 40) {
					joinPlayer(targetPlayer);
				}
			}
		}
		LogUtil.getMain().info(joinRecord.toString());
		LogUtil.getMain().info("war camp autoJoin suc ");
	}

	private synchronized static void stop() {
		LogUtil.getMain().info("war camp stop ...");
		if (vsTask != null) {
			vsTask.cancel(false);
		}
		EventNotice notice = new EventNotice(ConstantsRadio.camp, ConstantsRadio.campEnd);
		notice.broadcast(CampWar.getAllIds());
		clearOnEnd();
	}

	public static boolean isJoin(long uid) {
		return joinPlayers.containsKey(uid);
	}

	public static boolean isOpen() {
		return open;
	}

	public static void joinPlayer(Player player) {
		if (!joinPlayers.containsKey(player.getPlayerId())) {
			int area = CampConfig.getArea(player.getLevel());
			boolean source = joinRecord.get(area * 2 + 1) < joinRecord.get(area * 2 + 2);
			
			CampWarPlayer camPlayer = new CampWarPlayer();
			camPlayer.setId(player.getPlayerId());
			camPlayer.setArea(area);
			camPlayer.setSource(source);
			camPlayer.setSourceObj(player);			
			
			if (joinPlayers.putIfAbsent(camPlayer.getId(), camPlayer)== null) {
				joinRecord.incrementAndGet(camPlayer.getSign());
			}
		}

	}

	public static CampWarPlayer getCampPlayer(long uid) {
		return joinPlayers.get(uid);
	}

	public static Collection<CampWarPlayer> getAllWars() {
		return joinPlayers.values();
	}

	public static void quitPlayer(long uid) {
		CampWarPlayer camPlayer = joinPlayers.remove(uid);
		if (camPlayer != null) {
			joinRecord.decrementAndGet(camPlayer.getSign());
		}
	}
	public static void logoutUid(long uid){
		if (open) {
			CampWarPlayer camPlayer = joinPlayers.get(uid);
			if (camPlayer != null) {
				camPlayer.setAct(false);
			}
		}
	}
	

	public static Collection<Long> getAllIds() {
		return joinPlayers.keySet();
	}

	public static int getEndTime() {
		return endTime;
	}

}
